package com.love.testnetwork;

import android.util.Log;

import com.love.testnetwork.entity.BaseResponse;
import com.love.testnetwork.entity.GuestureEntity;
import com.love.testnetwork.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by zf on 2016/11/26.
 */

public class HttpUtils {
    private static final String TAG = "HttpUtils";

    /**
     * 请求数据的时候依赖上一个网络请求所需的参数
     */
    public static void test() {
        HttpMethods.getInstance().getInfo("58064", "64510d0f53a4465d9099ced1829eb18f").flatMap(new Func1<BaseResponse<UserEntity>, Observable<GuestureEntity>>() {
            @Override
            public Observable<GuestureEntity> call(BaseResponse<UserEntity> userEntityBaseResponse) {
                if (userEntityBaseResponse == null) {
                    return null;
                }
                UserEntity infolist = userEntityBaseResponse.getInfolist();

                if (infolist == null) {
                    return null;
                }

                String userid = infolist.getUSERID() + "";
                String imei = infolist.getVEHICLE_DATA();

                return HttpMethods.getInstance().getGuesture(userid, "64510d0f53a4465d9099ced1829eb18f", imei);
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<GuestureEntity>() {
                    @Override
                    public void call(GuestureEntity guestureEntity) {
                        Log.i(TAG, "call: " + guestureEntity.getValue());
                    }
                });


        List<GuestureEntity> entities = new ArrayList<>();
        entities.add(new GuestureEntity(1, 2));
        entities.add(new GuestureEntity(3, 4));


        List<GuestureEntity> entities2 = new ArrayList<>();
        entities2.add(new GuestureEntity(5, 6));
        entities2.add(new GuestureEntity(7, 8));

        Observable<GuestureEntity> list1 = Observable.from(entities);
        Observable<GuestureEntity> list2 = Observable.from(entities2);


        /**
         * 合并请求
         */
        rx.Observable.merge(list1, list2).subscribe(new Action1<GuestureEntity>() {
            @Override
            public void call(GuestureEntity guestureEntity) {
                Log.i(TAG, "call: " + guestureEntity.toString());
            }
        });


        /*rx.Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello");
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG, "call: " + s);
            }
        });*/
    }
}
